<?php

namespace app\school\controller;

use app\common\controller\BasicAdmin;
use app\common\model\CourseAttendanceModel;
use app\common\model\CourseModel;
use app\common\model\UserModel;
use app\common\model\UserTimesModel;
use app\common\traits\SelectPage;

class User extends BasicAdmin
{
    use SelectPage;
    public $model = 'UserModel';

    /**
     * 用户列表
     */
    public function index(){
        $school_id = session('school_user.school_id');
        return  $this->_query($this->model)
            ->where('school_id',$school_id)
            ->with(['school','schoolgrade','schoolclass'])
            ->like('nickname,phone')
            ->page();
    }

    /**
     * 详情
     */
    public function detail(){
        $user_id = $this->request->get('id');
        $user =  UserModel::where('id',$user_id)->with(['school','schoolgrade','schoolclass'])->find();
        //学生
        if($user['role']==1){
            //常规
            $user['normal'] = CourseModel::where('status',1)
                ->where('type',1)
                ->where('school_id',$user['school_id'])
                ->where('grade_id='.$user['grade_id'].' or grade_id is null')
                ->where('class_id='.$user['class_id'].' or class_id is null')
                ->select();
            //兴趣
            $course_ids = UserTimesModel::where('user_id',$user['id'])
                ->field('group_concat(course_id) as course_ids')->find()['course_ids'];
            $user['interest'] = CourseModel::where('status',1)
                ->where('type',2)
                ->where('id','in',$course_ids)
                ->select();
            //考勤
            $attendance = CourseAttendanceModel::where('find_in_set(in_userids,'.$user_id.')')->select();
            foreach ($attendance as $key=>$value){
                $value['course_name'] = CourseModel::where('id',$value['course_id'])->value('name');
            }
            $user['attendance'] = $attendance;
            //获取学生的学习时长与次数
            $user['count'] = CourseAttendanceModel::where('find_in_set('.$user['id'].',in_userids)')->count();
            $user['time'] =  ceil(45*$user['count']/60);
        }
        //教师
        if($user['role']==2){
            //常规
            $user['normal'] = CourseModel::where('status',1)
                ->where('type',1)
                ->where('teacher_id',$user['id'])
                ->select();
            //兴趣
            $course_ids = UserTimesModel::where('teacher_id',$user['id'])
                ->field('group_concat(course_id) as course_ids')->find()['course_ids'];
            $user['interest'] = CourseModel::where('status',1)
                ->where('type',2)
                ->where('id','in',$course_ids)
                ->select();
            //考勤
            $attendance = CourseAttendanceModel::where('teacher_id',$user['id'])->select();
            foreach ($attendance as $key=>$value){
                $value['course_name'] = CourseModel::where('id',$value['course_id'])->value('name');
            }
            $user['attendance'] = $attendance;
            //获取教师的授课时长与次数
            $user['count'] = CourseAttendanceModel::where('teacher_id',$user['id'])->count();
            $user['time'] =  ceil(45*$user['count']/60);
        }
        //班主任
        if($user['role']==3){
            //班主任的话计算学生数与课程数
            $user['student_count'] = UserModel::where('class_id',$user['class_id'])
                ->where('role',1)->count();
            $user['course_count'] =  CourseModel::where('class_id',$user['class_id'])->count();
            //缺勤率
            $course_ids = CourseModel::where('class_id',$user['class_id'])
                ->field('group_concat(id) as course_ids')->find()['course_ids'];
            $attendance_count = CourseAttendanceModel::where('course_id','in',$course_ids)->count();
            $attendance = CourseAttendanceModel::where('course_id','in',$course_ids)->select();
            $rate = 0;
            foreach ($attendance as $key=>&$value){
                if(count(explode(',',$value['all_userids']))!=0){
                    $rate += count(explode(',',$value['away_userids']))/count(explode(',',$value['all_userids']));
                }
            }
            $attendance_rate = $rate/$attendance_count;
            $user['attendance_rate'] = round($attendance_rate,4)*100;
        }
        //校长
        if($user['role']==4) {
            //校长的话计算学生数与班级数与缺勤率
            $user['student_count'] = UserModel::where('school_id', $user['school_id'])
                ->where('role', 1)->count();
            $user['class_count'] = UserModel::where('school_id', $user['school_id'])
                ->group('class_id')
                ->count();
            //缺勤率
            $course_ids = CourseModel::where('school_id', $user['school_id'])
                ->field('group_concat(id) as course_ids')->find()['course_ids'];
            $attendance_count = CourseAttendanceModel::where('course_id', 'in', $course_ids)
                ->where('away_userids', 'neq', null)
                ->where('all_userids', 'neq', null)
                ->count();
            $attendance = CourseAttendanceModel::where('course_id', 'in', $course_ids)->select();
            $rate = 0;
            foreach ($attendance as $key => &$value) {
                if ($value['away_userids'] != null && $value['all_userids'] != null && count(explode(',', $value['all_userids'])) != 0) {
                    $rate += count(explode(',', $value['away_userids'])) / count(explode(',', $value['all_userids']));
                }
            }
            $attendance_rate = $rate / $attendance_count;
            $user['attendance_rate'] = round($attendance_rate, 4) * 100;
        }
        $this->assign('user',$user);
        $this->fetch();
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $this->title = '编辑';
        return $this->_form($this->model, 'form');
    }

    /**
     * 设置真实姓名
     */
    public function set_realname()
    {
        $this->title = '编辑';
        return $this->_form($this->model, 'realname');
    }

    /**
     * 设置角色
     */
    public function set_role(){
        $user_id = $this->request->post('id');
        $role = $this->request->post('role');
        $this->_save($this->model, [
            'role' => $role,
        ],'id', ['id' => $user_id]);
    }

}